/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.util.ArrayList;
import java.util.StringTokenizer;

import com.nebrija.riif.model.ChildComponentList;
import com.nebrija.riif.model.ChildComponentUnit;
import com.nebrija.riif.model.Component;
import com.nebrija.riif.model.Environment;
import com.nebrija.riif.model.RIIFElement;
import com.nebrija.riif.model.RIIFProperties;
import com.nebrija.riif.model.RootClass;
import com.nebrija.riif.model.Timeline;

/**
 * Command to retrieve the description  of a RIIF element
 * @author asanchep
 *
 */
public class DescribeCommand implements Command {

	@Override
	public String execute(ArrayList<String> params) throws CommandException {
		// Search for the element
		RootClass r=RootClass.getInstance();
		// If no name is provided, return the root class information
		if(params.size()<1){
			return r.toString();
		}
		//The string must be like MEMORY'SBE'rate. Look for ' character
		StringTokenizer stk = new StringTokenizer(params.get(0),"'");
		
		
		//No elements
		if(!stk.hasMoreTokens()){
			return r.toString();
		}
		
		//Search for the base element
		RIIFElement element = r.getElement(stk.nextToken());
		if (element==null){
			throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
		}

		//Component/Environment/Requirement
		if(!stk.hasMoreTokens()){
			return element.toString();
		}		
		
		if(element instanceof Component){
			Component c = (Component)element;
			element = c.getElement(stk.nextToken());
			if (element==null){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}
		}else if (element instanceof Environment){
			Environment env = (Environment) element;
			element = env.getElement(stk.nextToken());
			if(element==null){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}
		}else if (element instanceof Timeline){
			Timeline time = (Timeline) element;
			element = time.getElement(stk.nextToken());
			if(element==null){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}
		}else{
			// Environment and requirements are not supported
			return "NOT SUPPORTED YET";
		}
		
		// ChildComponent/FailMode/...
		if(!stk.hasMoreTokens()){
			return element.toString();
		}	
		
		if(element instanceof ChildComponentUnit){
			ChildComponentUnit ccu = (ChildComponentUnit)element;
			element = ccu.getElement(stk.nextToken());
			if (element==null){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}			
		}
		
		if(element instanceof ChildComponentList){
			ChildComponentList ccu = (ChildComponentList)element;
			return ccu.toString();
		}
		
		if(element instanceof RIIFProperties){
			RIIFProperties prop = (RIIFProperties) element;
			Object value = prop.getProperty(stk.nextToken());
			if(value==null){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}
			if(stk.hasMoreTokens()){
				throw new CommandException(CommandException.ELEMENT_NOT_FOUND);
			}
			return value.toString();
		}

		return "NOT SUPPORTED YET"; 
		
	}

	@Override
	public String helpString() throws CommandException {
		return " <element'element...> : describes a specific element/value";
	}

}
